package com.taotao.item.controller;


import com.github.pagehelper.PageInfo;
import com.taotao.common.dto.Item;
import com.taotao.common.dto.TbItem;
import com.taotao.common.dto.TbItemDesc;
import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.item.service.ItemCatService;
import com.taotao.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品详情页面展示Controller
 * <p>Title: ItemController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class ItemController {
	
	@Autowired
	private ItemService itemService;
	@Autowired
	private ItemCatService itemCatService;

	@RequestMapping("/item1/{itemId}")
	@ResponseBody
	public TbItem getItemById1(@PathVariable Long itemId) {
		TbItem tbItem = itemService.getItemById(itemId);
		return tbItem;
	}

	@RequestMapping("/item/{itemId}")
	public String getItemById(@PathVariable Long itemId, Model model) {
		//取商品基本信息
		TbItem tbItem = itemService.getItemById(itemId);
		Item item = new Item(tbItem);
		//取商品详情
		TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);
		//把数据传递给页面
		model.addAttribute("item", item);
		model.addAttribute("itemDesc", tbItemDesc);
		//返回逻辑视图
		return "item";
	}

	@RequestMapping("/item/list")
	@ResponseBody
	public PageInfo<TbItem> getItemList(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows) {
		System.out.println(page+"+++++++++++++++++++++"+rows);
		PageInfo<TbItem> result = itemService.getItemList(page, rows);
		return result;
	}

	@PostMapping("/item/save")
	public TaotaoResult addItem(@RequestBody TbItem item, @RequestParam("desc") String desc) throws Exception {
		TaotaoResult result = itemService.addItem(item, desc);
		return result;
	}

	@GetMapping("/item/gen/{itemId1}")
	public String showItem1(@PathVariable Long itemId, Model model) {
		//取商品基本信息
		TbItem tbItem = itemService.getItemById(itemId);
		Item item = new Item(tbItem);
		//取商品详情
		TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);
		//把数据传递给页面
		model.addAttribute("item", item);
		model.addAttribute("itemDesc", tbItemDesc);
		//返回逻辑视图
		return "item";
	}
	@RequestMapping("/item/cat/list")
	@ResponseBody
	public List<EasyUITreeNode> getItemCatList(@RequestParam("parentId") Long parentId) {
		List<EasyUITreeNode> list = itemCatService.getItemCatList(parentId);
		return list;
	}
	@RequestMapping("/showItem/{itemId}")
	public String showItem(@PathVariable Long itemId, Model model) {
		//取商品基本信息
		TbItem tbItem = itemService.getItemById(itemId);
		Item item = new Item(tbItem);
		//取商品详情
		TbItemDesc tbItemDesc = itemService.getItemDescById(itemId);
		//把数据传递给页面
		model.addAttribute("item", item);
		model.addAttribute("itemDesc", tbItemDesc);
		//返回逻辑视图
		return "item";
	}
}
